/**
 *
 */
package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDrMessageConsultChargeService;
import com.naiterui.ehp.bs.doctor.utils.LimitValueConfig;
import com.naiterui.ehp.bs.doctor.vo.ConsultChargePatientGroupDetailVO;
import com.naiterui.ehp.bs.doctor.vo.ConsultOrderListVO;
import com.naiterui.ehp.bs.doctor.vo.ConsultSessionParamVO;
import com.naiterui.ehp.bs.doctor.vo.ConsultSessionVO;
import com.naiterui.ehp.bs.doctor.vo.LimitValueVO;
import com.naiterui.ehp.bs.doctor.vo.PatientGroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生图文咨询收费控制层
 * @author guoyongxiang
 * @since 2.7.0
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.SERVICE_CHARGE})
@RequestMapping("ad/charge/consult")
@RestController
@RequiredArgsConstructor
public class AdMessageConsultController {

    private final IDrMessageConsultChargeService drMessageConsultChargeService;

    private static final Logger LOGGER = LoggerFactory.getLogger(AdMessageConsultController.class);

    /**
     * 获取医生设置图文咨询费用信息接口
     * DrMessageConsultChargeController.getMessageConsultChargeInfo()
     * @Author xuchong
     * @Date 2017年4月2日
     * @since 2.7.0
     * @param doctorId
     * @param patientId
     * @return
     */
    @ApiIgnore
    @RequestMapping("info")
    @ResponseBody
    public Map<String, Object> getMessageConsultChargeInfo(Long doctorId, Long patientId) throws BusinessException {
        LOGGER.info("获取医生对患者设置图文咨询收费信息，参数：doctorId：{}; patientId:{}", doctorId, patientId);
        if (doctorId == null || patientId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Map<String, Object> retMap = new HashMap<>();
        // 获取医生对患者收费信息
        Integer price = this.drMessageConsultChargeService.getChargeById(doctorId, patientId);
        retMap.put("patientId", patientId);
        retMap.put("doctorId", doctorId);
        retMap.put("price", price);
        return retMap;
    }

    /**
     * 设置图文咨询收费价格
     * DrMessageConsultChargeController.setting()
     * @Author guoyongxiang
     * @Date 2017年4月2日
     * @since 2.7.0
     * @param doctorId
     * @param charge
     * @param selectedAll
     * @param groupIds
     * @param patientIds
     * @return
     */
    @ApiIgnore
    @ResponseBody
    @RequestMapping("setting")
    public void setting(Long doctorId, Integer charge, Integer selectedAll, String groupIds, String patientIds) throws BusinessException {
        LOGGER.info("setting start. doctorId {}, charge {}, selectedAll {}, groupIds {}, patientIds {}", doctorId, charge, selectedAll, groupIds,
                patientIds);

        LimitValueVO limitValueVO = LimitValueConfig.getLimitConfig();
        Integer maxCharge = limitValueVO.getMessageChargeAmount().getMax();
        Integer minCharge = limitValueVO.getMessageChargeAmount().getMin();
        minCharge = minCharge == null ? 0 : minCharge;
        if (charge < minCharge || charge > maxCharge || selectedAll == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        this.drMessageConsultChargeService.saveCharge(doctorId, charge, selectedAll, groupIds, patientIds);

    }

    /**
     * 根据医生ID获取患者分组
     * DrMessageConsultChargeController.getPatientGroups()
     * @Author guoyongxiang
     * @Date 2017年4月2日
     * @since 2.7.0
     * @param doctorId
     * @return
     */
    @ApiIgnore
    @ResponseBody
    @RequestMapping("patient/groups")
    public List<PatientGroupVo> getPatientGroups(Long doctorId) {
        LOGGER.info("getPatientGroups start. doctorId {}", doctorId);

        return this.drMessageConsultChargeService.getPatientGroups(doctorId);
    }

    /**
     * 根据分组ID获取组内患者列表
     * DrMessageConsultChargeController.getPatients()
     * @Author guoyongxiang
     * @Date 2017年4月2日
     * @since 2.7.0
     * @param groupId
     * @param doctorId
     * @return
     * @throws BusinessException
     */
    @ApiIgnore
    @ApiOperation(value = "图文咨询患者分组详情", notes = "根据分组ID查询图文咨询患者分组详情列表", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "groupId", value = "分组ID", required = true, dataType = "long", paramType = "query")})
    @ResponseBody
    @RequestMapping(value = "patients")
    public ResponseVO<List<ConsultChargePatientGroupDetailVO>> getPatients(Long groupId, Long doctorId) throws Exception {
        LOGGER.info("getPatients start. groupId {}, doctorId {}", groupId, doctorId);
        if (groupId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        long start = System.currentTimeMillis();

        List<ConsultChargePatientGroupDetailVO> groupPatientVO = this.drMessageConsultChargeService.getPatientsByGroupId(groupId, doctorId);

        LOGGER.info("getPatients end. cost:{}. doctorId {}", System.currentTimeMillis() - start, doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, groupPatientVO);
    }


    @ApiOperation(value = "我的诊疗列表")
    @PostMapping("list")
    public ResponseVO<PageVO<ConsultSessionVO>> consultSessionList(ConsultSessionParamVO paramVO, PageParamVO pageParamVO) throws BusinessException {

        PageVO<ConsultSessionVO> pageVO = this.drMessageConsultChargeService.consultSessionList(paramVO, pageParamVO);
        return PublicService.returnResponseVO(pageVO);
    }

    /**
     * （医生端优化1.1）新增诊疗收入列表
     * @param paramVO
     * @param pageParamVO
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "医生端1.1优化我的诊疗收入")
    @PostMapping("list/income")
    public ResponseVO<ConsultOrderListVO> consultIncome(ConsultSessionParamVO paramVO, PageParamVO pageParamVO) throws BusinessException {

        LOGGER.info("医生端诊疗收入查询 paramVO={}，pageParamVO={}", paramVO, pageParamVO);
        return PublicService.returnResponseVO(this.drMessageConsultChargeService.consultIncome(paramVO, pageParamVO));

    }


}
